/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of libgp2x.

    libgp2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libgp2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libgp2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "gp2x_internal.h"

unsigned int GP2x_current_input_state;

void GP2x_input_init() {
    GP2x_current_input_state = 0;
}

unsigned int GP2x_input_poll() {
    SDL_Event event;

    while(SDL_PollEvent(&event)) {
        switch(event.type) {
        case SDL_KEYDOWN:
            if(event.key.keysym.sym == SDLK_w) {
                GP2x_current_input_state |= JOYSTICK_UP;
            } else if(event.key.keysym.sym == SDLK_q) {
                GP2x_current_input_state |= JOYSTICK_UPLEFT;
            } else if(event.key.keysym.sym == SDLK_a) {
                GP2x_current_input_state |= JOYSTICK_LEFT;
            } else if(event.key.keysym.sym == SDLK_z) {
                GP2x_current_input_state |= JOYSTICK_DOWNLEFT;
            } else if(event.key.keysym.sym == SDLK_x) {
                GP2x_current_input_state |= JOYSTICK_DOWN;
            } else if(event.key.keysym.sym == SDLK_c) {
                GP2x_current_input_state |= JOYSTICK_DOWNRIGHT;
            } else if(event.key.keysym.sym == SDLK_d) {
                GP2x_current_input_state |= JOYSTICK_RIGHT;
            } else if(event.key.keysym.sym == SDLK_e) {
                GP2x_current_input_state |= JOYSTICK_UPRIGHT;
            } else if(event.key.keysym.sym == SDLK_s) {
                GP2x_current_input_state |= JOYSTICK_PUSH;
            } else if(event.key.keysym.sym == SDLK_BACKSLASH) {
                GP2x_current_input_state |= VOLUME_DOWN;
            } else if(event.key.keysym.sym == SDLK_SLASH) {
                GP2x_current_input_state |= VOLUME_UP;
            } else if(event.key.keysym.sym == SDLK_i) {
                GP2x_current_input_state |= BUTTON_Y;
            } else if(event.key.keysym.sym == SDLK_j) {
                GP2x_current_input_state |= BUTTON_A;
            } else if(event.key.keysym.sym == SDLK_k) {
                GP2x_current_input_state |= BUTTON_B;
            } else if(event.key.keysym.sym == SDLK_m) {
                GP2x_current_input_state |= BUTTON_X;
            } else if(event.key.keysym.sym == SDLK_b) {
                GP2x_current_input_state |= SELECT;
            } else if(event.key.keysym.sym == SDLK_n) {
                GP2x_current_input_state |= START;
            }
            break;
        case SDL_KEYUP:
            if(event.key.keysym.sym == SDLK_w) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_UP);
            } else if(event.key.keysym.sym == SDLK_q) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_UPLEFT);
            } else if(event.key.keysym.sym == SDLK_a) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_LEFT);
            } else if(event.key.keysym.sym == SDLK_z) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_DOWNLEFT);
            } else if(event.key.keysym.sym == SDLK_x) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_DOWN);
            } else if(event.key.keysym.sym == SDLK_c) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_DOWNRIGHT);
            } else if(event.key.keysym.sym == SDLK_d) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_RIGHT);
            } else if(event.key.keysym.sym == SDLK_e) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_UPRIGHT);
            } else if(event.key.keysym.sym == SDLK_s) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | JOYSTICK_PUSH);
            } else if(event.key.keysym.sym == SDLK_BACKSLASH) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | VOLUME_DOWN);
            } else if(event.key.keysym.sym == SDLK_SLASH) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | VOLUME_UP);
            } else if(event.key.keysym.sym == SDLK_i) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | BUTTON_Y);
            } else if(event.key.keysym.sym == SDLK_j) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | BUTTON_A);
            } else if(event.key.keysym.sym == SDLK_k) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | BUTTON_B);
            } else if(event.key.keysym.sym == SDLK_m) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | BUTTON_X);
            } else if(event.key.keysym.sym == SDLK_b) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | SELECT);
            } else if(event.key.keysym.sym == SDLK_n) {
                GP2x_current_input_state = ~((~GP2x_current_input_state) | START);
            }
            break;
        case SDL_QUIT:
            exit(0);
        }
    }
    return GP2x_current_input_state;
}
